package com.css.fxfzmhwz.base.dict.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzmhwz.base.dict.repository.entity.DictItemEntity;
import com.css.fxfzmhwz.base.dict.service.DictItemService;
import com.css.fxfzmhwz.base.dict.viewobjects.DictEntityVo;
import com.css.fxfzmhwz.base.dict.viewobjects.DictItemQueryParams;
import com.css.fxfzmhwz.base.response.RestResponse;
import com.css.fxfzmhwz.constants.FxfzConstants;
import com.css.fxfzmhwz.util.PlatformPageUtils;
import com.css.fxfzmhwz.util.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/test/dictItems")
@Slf4j
public class DictItemController {
    @Autowired
    DictItemService dictItemService;

    @Autowired
    RedisUtils redisUtils;

    @GetMapping("queryDictItems")
    public RestResponse queryDictItems(HttpServletRequest request, DictItemQueryParams queryParams) {
        RestResponse response = null;
        JSONObject jsonObject = new JSONObject();
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            jsonObject = dictItemService.queryDictItems(queryParams,curPage,pageSize);
            response = RestResponse.succeed(jsonObject);
        }catch (Exception e){
            log.error("查询字典项失败!",e);
            response = RestResponse.fail("查询字典项失败!");
        }
        return response;
    }

    @GetMapping("{id}")
    public RestResponse getDictItem(@PathVariable String id) {
        RestResponse response = null;
        DictItemEntity die = null;
        try{
             die = dictItemService.getDictItem(id);
            response = RestResponse.succeed(die);
        }catch (Exception e){
            log.error("获取字典项失败!",e);
            response = RestResponse.fail("获取字典项失败!");
        }
        return response;
    }
    @PostMapping
    public RestResponse saveDictItem(@RequestBody DictItemEntity itemEntity) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictItemService.saveDictItem(itemEntity);
            json.put("message", "新增字典项成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("新增字典项失败!",e);
            json.put("message", "新增字典项失败!");
            response = RestResponse.succeed(json);
        }
        return response;

    }
    @PutMapping
    public RestResponse updateDictItem(@RequestBody DictItemEntity itemEntity)  {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictItemService.updateDictItem(itemEntity);
            json.put("message", "修改字典项成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("修改字典项失败!",e);
            json.put("message", "修改字典项失败!");
            response = RestResponse.succeed(json);
        }
        return response;
    }
    @DeleteMapping("{ids}")
    public RestResponse deleteDictItems(@PathVariable("ids") List<String> ids) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        try{
            dictItemService.deleteDictItems(ids);
            json.put("message", "删除字典项成功!");
            response = RestResponse.succeed(json);
        }catch (Exception e){
            log.error("删除字典项失败!",e);
            json.put("message", "删除字典项失败!");
            response = RestResponse.succeed(json);
        }
        return response;
    }
    @GetMapping("updateDictItemsType/{ids}/{type}")
    public RestResponse updateDictItemsType(@PathVariable("ids") List<String> ids, @PathVariable("type")String type) {
        RestResponse response = null;
        JSONObject json = new JSONObject();
        if("1".equals(type)){
            try{
                dictItemService.updateDictItems(ids,type);
                json.put("message", "激活字典项成功!");
                response = RestResponse.succeed(json);
            }catch (Exception e){
                log.error("激活字典项失败!",e);
                json.put("message", "激活字典项失败!");
                response = RestResponse.succeed(json);
            }
        }else{
            try{
                dictItemService.updateDictItems(ids,type);
                json.put("message", "禁用字典项成功!");
                response = RestResponse.succeed(json);
            }catch (Exception e){
                log.error("禁用字典项失败!",e);
                json.put("message", "禁用字典项失败!");
                response = RestResponse.succeed(json);
            }
        }
        return response;
    }
    @GetMapping("validateDictItemCode")
    public RestResponse validateDictItemCode(@RequestParam String dictId, @RequestParam String dictItemId, @RequestParam String dictItemCode) {
        RestResponse response = null;
        boolean isValid = false;
        try{
            isValid = dictItemService.validateDictItemCode(dictId,dictItemId,dictItemCode);
            response = RestResponse.succeed(isValid);
        }catch (Exception e){
            log.error("校验字典项编码唯一性失败!",e);
            response = RestResponse.fail("校验字典项编码唯一性失败!");
        }
        return response;
    }

    /**
     * 根据字典编码获取对应的字典项
     */
    @GetMapping("/getDictItemByDictId/{dictCode}")
    public RestResponse getDictItemByDictId(@PathVariable String dictCode) {
        RestResponse response = null;
        List<DictEntityVo> vos = null;
        try{
            String jsonStr = redisUtils.getCache(FxfzConstants.CACHE_SYS_KEY  + dictCode);
            if(StringUtils.isBlank(jsonStr)){
                //从数据库查询，并放到内存中去
                vos = dictItemService.getDictItemByDictId(dictCode);
            }else{
                //直接从内存拿
                String parse =(String) JSONObject.parse(jsonStr);
                vos = JSONArray.parseArray( parse, DictEntityVo.class);
            }

            response = RestResponse.succeed(vos);
        }catch (Exception e){
            log.error("获取字典项失败!",e);
            response = RestResponse.fail("获取字典项失败!");
        }
        return response;
    }

}